define(window.backboneCommon.getViewDefine("customer.index"), function ($,require,Collection,tpl) {
    var backboneView= Backbone.View.extend({
        events: {
            "click .global-listParam":"saveGlobalListParam"
        },
        initialize: function () {
            this.listenTo(this.model, 'bind:PageList', this.bindList);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(){
            this.elementModel={};
            this.extObject={};
            this.pageData={listParam:{},initExtObject:{}};
            this.elementModel.table=$("#tb_list");
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("list",["create","imitateLogin"],function(json){
                self.pageData.powerInfo=json;
                self.pageData.power={
                    imitateLogin:json["imitateLogin"]
                };
                self._initBodyMain();
            });
        },
        _initBodyMain:function(){
            this._initAssistCallback();
            this._getPageGlobalParam();
            this._initInputSearch();
            this._initPageList();
        },
        _initAssistCallback:function(){
            var self=this;
            $.ExtAssist.winUnload.callback(function(){
                self._setPageGlobalParam(null)
            });
        },
        _getPageGlobalParam:function(){
            var data=$.ExtAssist.pageListParam.getData("CustomerList");
            if(data.search){$("#panel_inputSearch").data("initValue",data.search)}
            this.pageData.listParam=data;
        },
        _setPageGlobalParam:function(param){
            $.ExtAssist.pageListParam.setData("CustomerList",param);
        },
        saveGlobalListParam:function(){
            var param=$.ExtCommon.getJsonClone(this.pageData.listParam);
            param.offset=this.extObject.table.getCurrentOffset();
            this._setPageGlobalParam(param);
        },
        _initInputSearch:function () {
            var self=this;
            self.extObject.inputSearch=$("#panel_inputSearch").ExtInputSearch({
                placeholder:"广告主ID,广告主名称",
                searchCallback:function () {
                    self.extObject.table.refresh()
                }
            })
        },
        _getSearchParam:function(){
            var param={};
            var keyword = this.extObject.inputSearch.getValue();
            if (!$.ExtCommon.isCheckEmpty(keyword)) {param.search = keyword;}
            return param;
        },
        _initPageList: function () {
            var self=this;
            var options={
                columns: [
                    {data: "id",class: "w-60"},
                    {data: "name",class: "w-min-100"},
                    {data: "statusName",class: "w-200"},
                    {data: "createTime",class: "w-120"},
                    {data: null,class: "w-70 text-center"}
                ],
                infoExt:{
                    ajaxDataPara:function() {
                        var param = {};
                        param = $.ExtCommon.initMergeJson(param, self._getSearchParam());
                        self.pageData.listParam = $.ExtCommon.getJsonClone(param);
                        return param;
                    },
                    ajaxWholeCallback:function(param){
                        self.collection.getPageList(param)
                    }
                },
                "createdRow": function (row, data) {
                    var elementTd = $('td', row);
                    elementTd.eq(1).html($.ExtCommon.getStringConnect('<a class="btn-operate longterm global-listParam" href="#customer/detail/{0}">{1}</a>', [data.id,data.name]));
                    var statusColor = "";
                    if (data.isSuccess) {
                        statusColor = "text-green"
                    } else if (data.isFail||data.isFrozen) {
                        statusColor = "text-red"
                    }
                    var statusHtml = data.isFail
                        ? $.ExtCommon.getStringConnect('<span class="{1} customer-status-msg" data-toggle="tooltip" data-placement="right" data-container="body"  title="{2}">{0}</span>', [data.statusName, statusColor, data.errMsg])
                        : $.ExtCommon.getStringConnect('<span class="{1}">{0}</span>', [data.statusName, statusColor]);
                    elementTd.eq(2).html(statusHtml);
                    if(self.pageData.power.imitateLogin){
                        elementTd.eq(4).html($.ExtCommon.getStringConnect('<a class="btn-operate longterm" href="#customer?imitateId={0}" target="_blank">模拟登陆</a>', [data.id]));
                    }
                },
                "drawCallback": function () {
                    self.elementModel.table.find(".customer-status-msg").tooltip();
                    $.ExtAssist.urlParam.setUrl();
                }
            };
            if(!$.ExtCommon.isCheckEmpty(self.pageData.listParam.offset)){options.displayStart=self.pageData.listParam.offset}
            options.columns[4]["visible"]=self.pageData.power.imitateLogin;
            self.extObject.table=self.elementModel.table.ExtDataTable(options);
        },
        bindList:function(){
            var data=this.model.get("dataPageList");
            this.extObject.table.ajaxWholeRefreshData(data);
        }
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el: "#container_body",collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function () {
            this.view.initMain();
        }
    };
    return viewModel;
});